package com.arvin.controller;

import com.arvin.pojo.*;
import com.arvin.service.ExamService;
import com.arvin.service.LogService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
@Tag(name = "考试相关接口",description = "考试相关接口")
@Slf4j
public class ExamController {

    @Autowired
    private ExamService examService;
    @Autowired
    private LogService logService;

    /**
     * 获取学生考试信息
     * @param page
     * @param pageSize
     * @param studentId
     * @param stuName
     * @param className
     * @param examDate
     * @param examState
     * @param examScore
     * @return
     */
    @Operation(summary = "获取学生考试信息",description = "获取学生考试信息")
    @GetMapping("/exam/getExamInfo")
    public Result<PageBean> getExamInfo(Integer page, Integer pageSize, String studentId,
                                        String stuName,String className, LocalDate examDate,
                                        String examState,Integer examScore) {
        // 默认考试时间排序asc升序、查询,分页
        PageBean pageBean = examService.getExamInfo(page, pageSize, studentId,
                stuName,className, examDate, examState,examScore);
        log.info("考试信息{}", pageBean);
        return Result.success(pageBean);
    }

    /**
     * 学生考试成绩打分
     */
    @Operation(summary = "学生考试成绩打分")
    @PutMapping("/exam/setExamScore")
    public Result setExamScore(@RequestBody Exam exam) {
        Integer examId = exam.getExamId();
        Integer examScore = exam.getExamScore();
        String examState = exam.getExamState();
        examService.updateExamState(examId, examState,examScore);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("考试");
        log.setLogOperate("管理员" + username + "为学号为" + exam.getStudentId()
                + "的学生进行考试打分:" + examScore + "并修改考试状态为：" + examState);
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生批量通过考试
     * @param ids
     * @return
     */
    @Operation(summary = "学生批量通过考试")
    @PutMapping("/exam/passByExamIds/{ids}")
    public Result passByExamIds(@PathVariable List<Integer> ids) {
        examService.passByExamIds(ids,"已通过");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("考试");
        log.setLogOperate("管理员" + username + "将考试id为" + ids
                + "的考试状态修改为：" + "已通过");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生批量不通过考试
     * @param ids
     * @return
     */
    @Operation(summary = "学生批量不通过考试")
    @PutMapping("/exam/noPassByExamIds/{ids}")
    public Result noPassByExamIds(@PathVariable List<Integer> ids) {
        examService.passByExamIds(ids,"未通过");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("考试");
        log.setLogOperate("管理员" + username + "将考试id为" + ids
                + "的考试状态修改为：" + "未通过");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }
}
